// map.init.ts
import * as Cesium from 'cesium';
import { Img, Img1 } from './map.config';
import jinanGeoJSON from '@/utils/jinanxzqh.json';
import { useCesiumClick } from '@/utils/useCesiumClick';
import { useCesiumPoints } from '@/hooks/useCesiumPoints';
import componentsTitle from '@/constants/components/componentsTitle';
import { useAssistStore } from '@/store/modules/Assist/useAssistStore';
import { useCesiumEntity } from '@/views/IntelligentAnalytics/config/useCesiumEntity';
const { addGeo } = useCesiumEntity();

export const mapInit = (cesiumContainer, emit) => {
  const route = useRoute();
  window.viewer = new Cesium.Viewer(cesiumContainer, {
    baseLayer: false,
    geocoder: false, // 搜索框
    homeButton: false, // home按钮
    sceneModePicker: false, // 3d/2d 模式切换按钮
    baseLayerPicker: false, // 图层选择按钮
    navigationHelpButton: false, // 右上角的帮助按钮
    animation: false, // 左下角的动画控件的显示
    shouldAnimate: false, // 控制模型动画
    timeline: false, // 底部的时间轴
    fullscreenButton: false, // 右下角的全屏按钮
    selectionIndicator: false, // 选择指示器
    infoBox: false, // 信息面板
    contextOptions: {
      webgl: {
        alpha: true
      }
    }
  });
  emit('init');
  window.viewer.imageryLayers.removeAll();
  // 天地图影像
  window.viewer.imageryLayers.addImageryProvider(Img);
  // 天地图影像注记
  window.viewer.imageryLayers.addImageryProvider(Img1);
  window.viewer.camera.setView({
    // Cesium的坐标是以地心为原点，一向指向南美洲，一向指向亚洲，一向指向北极州
    // fromDegrees()方法，将经纬度和高程转换为世界坐标
    destination: Cesium.Cartesian3.fromDegrees(117.140035075, 36.798747059, 400000)
  });
  // 创建Viewer后启用深度检测
  window.viewer.scene.screenSpaceCameraController.enableTilt = true;
  // 如果为真，允许用户放大和缩小。如果为假，相机将锁定到距离椭圆体的当前距离
  window.viewer.scene.screenSpaceCameraController.enableZoom = true;
  regionalMask();
  useCesiumClick(window.viewer, (e) => {
    if (!e.id.description) return;
    const description = JSON.parse(e.id.description.getValue());
    if (description.moduleTitle === '浓度点') {
      ElMessageBox.confirm('是否确定删除该点位', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        useAssistStore().removeConcentrationPoint(description.moduleId);
      });
    } else if (description.moduleTitle === '区域') {
      ElMessageBox.confirm('是否确定删除该区域', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        useAssistStore().removeArea(description.moduleId);
      });
    } else {
      if (description.moduleTitle === componentsTitle.INCIDENT_DISPOSITION) {
        if (route.path === '/assistantDecision') {
          useAssistStore().currentWarningId = description.moduleId;
          return;
        }
      } else if (description.moduleTitle === componentsTitle.ALARM_TYPE) {
        if (route.path === '/IntelligentAnalytics') {
          return;
        }
      } else if (description.moduleTitle === '救援资源') {
        addGeo(description.geo_resource[2].value, description.name);
      }
      useCesiumPoints().createPopUp(e.id);
    }
  });
};

export const regionalMask = () => {
  const maskpointArray = [];
  const coordinates = jinanGeoJSON.features[0].geometry.coordinates[0][0];
  for (let i = 0; i < coordinates.length; i++) {
    maskpointArray.push(coordinates[i][0]);
    maskpointArray.push(coordinates[i][1]);
  }
  const maskspoint = Cesium.Cartesian3.fromDegreesArray(maskpointArray);

  // 创建外部区域的坐标数组
  const outerBoundary = Cesium.Cartesian3.fromDegreesArray([0, 0, 10, 89, 160, 89, 160, 0]);

  // 创建多边形层次结构，包含外部区域和挖空区域
  const polygonHierarchy = new Cesium.PolygonHierarchy(outerBoundary, [new Cesium.PolygonHierarchy(maskspoint)]);

  const area = new Cesium.Entity({
    id: new Date() + '2',
    polygon: {
      hierarchy: polygonHierarchy,
      material: Cesium.Color.fromCssColorString('#5ec0f0').withAlpha(0.6) // 外部颜色
    }
  });

  window.viewer.entities.add(area);
};

export const mapDestroy = () => {
  if (window.viewer) {
    window.viewer.destroy();
    window.viewer = null;
  }
};
